﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AzukiMap.Tools.Logger;
using System.Collections.Generic;
using AzukiMap.MapSource;

namespace AzukiMapUI
{
    public delegate void MapSourceSelectedHandler(object sender, MapSourceSelectedArgs e);

	public partial class LayerSelectDialog : UserControl
	{
        public event MapSourceSelectedHandler MapSourceSelected;
        protected virtual void OnMapSourceSelected(MapSourceSelectedArgs e)
        {
            if (MapSourceSelected != null)
            {
                MapSourceSelected(this, e);
            }
        }

        IMapSourceProperties _selectedProp;
        List<IMapSourceProperties> _RasterProps;
        List<IMapSourceProperties> _VectorProps;
        List<IMapSourceProperties> _TileProps;
       
		public LayerSelectDialog()
		{
			// Required to initialize variables
			InitializeComponent();
            SetPropList();
		}

        void SetPropList()
        {
            _RasterProps = new List<IMapSourceProperties>();
            _RasterProps.Add(PropWMSLayer);
            //_RasterProps.Add(PropImageSource);

            _VectorProps = new List<IMapSourceProperties>();
            _VectorProps.Add(PropPostGISTable);

            _TileProps = new List<IMapSourceProperties>();
            _TileProps.Add(PropKaMapTile);
            _TileProps.Add(PropVirtualEarthTile);
            //_TileProps.Add(PropGoogleMapTile);

            _selectedProp = PropPostGISTable;
        }

        #region handlers

        void CbRasterDataType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int selectedIndex = ((ComboBox)sender).SelectedIndex;

            // To ignore a event on init time.
            //if (PropWMSLayer != null && PropImageSource != null)
            if (PropWMSLayer != null )
            {
                SetProperties(selectedIndex, _RasterProps);
            }
        }

        void CbVectorDataType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int selectedIndex = ((ComboBox)sender).SelectedIndex;

            // To ignore a event on init time.
            if (PropPostGISTable != null)
            {
                SetProperties(selectedIndex, _VectorProps);
            }

        }

        void CbTileDataType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int selectedIndex = ((ComboBox)sender).SelectedIndex;

            // To ignore a event on init time.
            if (PropKaMapTile != null) // && 
                //PropVirtualEarthTile != null && 
                //PropGoogleMapTile != null)
            {
                SetProperties(selectedIndex, _TileProps);
            }
        }

        void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            //WndLayerSelect.Close();
            this.Visibility = Visibility.Collapsed;
        }

        void BtnOk_Click(object sender, RoutedEventArgs e)
        {
            if (! _selectedProp.CheckAllFormData())
            {
                return;
            }

            MapSource selectedSource = _selectedProp.MapDataSource;
            MapSourceSelectedArgs ea = new MapSourceSelectedArgs(selectedSource);
            OnMapSourceSelected(ea);
            this.Visibility = Visibility.Collapsed;
        }

        void TcDataType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string selectedTabHeader = ((TabItem)(((TabControl)sender).SelectedItem)).Name;
            SetProperties(selectedTabHeader);
        }

        #endregion

        void SetProperties(int selectedIndex, List<IMapSourceProperties> props)
        {
            foreach (IMapSourceProperties prop in props)
            {
                prop.Visibility = Visibility.Collapsed;
            }
            _selectedProp = props[selectedIndex];
            props[selectedIndex].Visibility = Visibility.Visible;
        }

        void SetProperties(string selectedTabHeader)
        {
            List<IMapSourceProperties> props = null;
            ComboBox combo = null;
            switch (selectedTabHeader)
            {
                case "Vector":
                    props = _VectorProps;
                    combo = CbVectorDataType;
                    break;
                case "Raster":
                    props = _RasterProps;
                    combo = CbRasterDataType;
                    break;
                case "Tile":
                    props = _TileProps;
                    combo = CbTileDataType;
                    break;
            }
            if (props == null || combo == null)
            {
                return;
            }
            int selectedComboIndex = combo.SelectedIndex;
            SetProperties(selectedComboIndex, props);
        }

	}

    public class MapSourceSelectedArgs : EventArgs
    {
        MapSource _mapSource;

        public MapSource MapSource
        {
            get
            {
                return _mapSource;
            }
        }

        public MapSourceSelectedArgs(MapSource mapSource)
        {
            _mapSource = mapSource;
        }
    }
}